<?php

class Image{
								//定义属性
	//保存路径
	protected $savePath;
	//是否使用随机名
	protected $randName;
	//图片后缀名
	protected $extension;
	//保存文件名
	protected $saveFileName;
	


								//构造方法
	function __construct($savePath='./',$randName=true,$extension=false) {
		$this -> savePath = rtrim($savePath,'/') . '/';
		$this -> extension = $extension;
		$this -> randName = $randName;
	}




								//添加水印-----------------------------------
	//添加水印静态方法
	static function waterMark(	$dstpath,                        //背景图片
							 	$srcpath,						 //水印图片
							 	$pos=9,							 //水印位置
							 	$pct=100,						 //水印透明度
							 	$savePath='./',					 //保存路径
							 	$randName=true,					 //是否使用随机名字
							 	$extension=false 				 //可自主设置文件后缀
							 ) {
		$obj = new self($savePath,$randName,$extension);
		$set = $obj -> setWaterMark($dstpath,$srcpath,$pos,$pct);
		return $set;
	}
	//添加水印
	protected function setWaterMark($dstpath,$srcpath,$pos=9,$pct=100) {
		//检查目录和文件
		if (!is_dir($this -> savePath)) {
			return '保存路径不存在';
		}
		if (!is_writable($this -> savePath)) {
			return '保存路径不可写';
		}
		if (!file_exists($dstpath)) {
			return '目标背景图片不存在';
		}
		if (!file_exists($srcpath)) {
			return '水印图片不存在';
		}
		//获得图片尺寸
		list($dstWidth,$dstHeight) = getimagesize($dstpath);
		list($srcWidth,$srcHeight) = getimagesize($srcpath);
		if ($srcWidth > $dstWidth || $srcHeight > $dstHeight) {
			return '水印图片过大';
		}
		//计算水印在图片上的位置
		if ($pos >= 1 && $pos <= 9) {
			$offsetX = ($dstWidth-$srcWidth)/2 * (($pos - 1)%3);
			$offsetY = ($dstHeight-$srcHeight)/2 * floor(($pos - 1)/3);
		} else {
			$offsetX = mt_rand(0,$dstWidth-$srcWidth);
			$offsetY = mt_rand(0,$dstHeight-$srcHeight);
		}
		//将水印合并到图片上
		$dstImg = $this -> openImage($dstpath);
		$srcImg = $this -> openImage($srcpath);
		imagecopymerge($dstImg, $srcImg, $offsetX, $offsetY, 0, 0, $srcWidth, $srcHeight, $pct);
		//保存图片
		$this -> saveImage($dstImg,$dstpath);
		//释放资源
		imagedestroy($dstImg);
		imagedestroy($srcImg);
		//返回保存图片的路径
		return $this->saveFileName;
	}





								//缩放图片------------------------------------
	//缩放图片静态方法
	static function zoomImage( $imgPath,						//要缩放的图片
							   $width,							//要缩放的宽度
							   $height,							//要缩放的高度
							   $savePath='./',					//保存路径
							   $randName=true,					//是否使用随机名字
							   $extension='png'					//保存文件的后缀
							) {
		$obj = new Image($savePath,$randName,$extension);
		$set = $obj -> setZoomImage($imgPath,$width,$height);
		return $set;
	}
	//缩放图片
	protected function setZoomImage($imgPath,$width,$height) {
		//检查目录和文件
		if (!file_exists($imgPath)) {
			return '图片路径不存在';
		} else if (!is_dir($this->savePath)) {
			return '保存路径不存在';
		} else if (!is_writable($this->savePath)) {
			return '保存路径不可写';
		}
		//计算尺寸
		list($srcWidth,$srcHeight) = getimagesize($imgPath);
		$size = $this->getSize($width,$height,$srcWidth,$srcHeight);
		//合并图片
		$dstImg = imagecreatetruecolor($width,$height);
		$srcImg = $this->openImage($imgPath);
		$this->mergeImage($dstImg,$srcImg,$size);
		// //保存图片
		$this->saveImage($dstImg,$imgPath);
		//释放资源
		imagedestroy($dstImg);
		imagedestroy($srcImg);
		//返回保存的文件路径名
		return $this->saveFileName;
	}
	
	//计算尺寸
	protected function getSize($width,$height,$srcWidth,$srcHeight) {
		//保存原始尺寸
		$size['srcWidth'] = $srcWidth;
		$size['srcHeight'] = $srcHeight;
		//计算缩放比例
		$scaleWidth = $width / $srcWidth;
		$scaleHeight = $height / $srcHeight;
		$scaleFinal = min($scaleWidth,$scaleHeight);
		//保存实际尺寸
		$size['newWidth'] = $srcWidth * $scaleFinal;
		$size['newHeight'] = $srcHeight * $scaleFinal;
		//计算偏移尺寸
		if ($scaleWidth < $scaleHeight) { 
			$size['offsetX'] = 0;
			$size['offsetY'] = round(($height - $size['newHeight'])/2);
		} else {
			$size['offsetY'] = 0;
			$size['offsetX'] = round(($width - $size['newWidth'])/2);
		}	
		return $size;
	}

	//合并图片
	protected function mergeImage($dstImg,$srcImg,$size)
	{
		//获取原始图片的透明色
		$lucidColor = imagecolortransparent($srcImg);
		if ($lucidColor == -1) {
			//如果没有透明色，默认设置白色为透明色
			$lucidColor = imagecolorallocate($dstImg, 255, 255, 255);
		}
		//用透明色填充图片
		imagefill($dstImg, 0, 0, $lucidColor);
		//设置透明色
		imagecolortransparent($dstImg,$lucidColor);
		//合并图片
		imagecopyresampled($dstImg, $srcImg, $size['offsetX'], $size['offsetY'], 0, 0, $size['newWidth'], $size['newHeight'], $size['srcWidth'], $size['srcHeight']);
	}
								//
								





								//公共方法------------------------------------
	//打开图片
	protected function openImage($imagePath) {
		$imageInfo = getimagesize($imagePath);
		$extension = image_type_to_extension($imageInfo[2],false);
		$openFunc = 'imagecreatefrom' . $extension;
		return $openFunc($imagePath);
	}
	//保存图片
	protected function saveImage($image,$oldimage) {
		//路径
		$this -> saveFileName = $this -> savePath;
		//名字
		$info = pathinfo($oldimage);
		if ($this -> randName) {
			$this -> saveFileName .= uniqid();
		} else {
			$this -> saveFileName .= $info['filename'];
		}
		//后缀
		if (!$this -> extension) {
			$this -> extension = $info['extension'];
		} else {
			$this -> extension = ltrim($this -> extension,'.');
		}
		//拼接完整路径名
		$this -> saveFileName .= '.' . $this -> extension;
		//保存图片
		if ($this -> extension == 'jpg') {
			$this -> extension = 'jpeg';
		}
		$saveFunc = 'image' . $this -> extension;
		$saveFunc($image,$this -> saveFileName);
		return $this -> saveFileName;
	}
								//
}


// 缩放图片用法
// $set = Image::zoomImage('20131212095323673.jpg',200,200);
// echo $set;

// 添加水印用法
// $set = Image::waterMark('20131212095323673.jpg','80T58PICheN_1024.jpg',1);
// echo $set;